using System.Collections.Generic;
using System.Linq;

namespace ReverseVowelsofaString
{
    public class Solution
    {
        public string ReverseVowels(string s)
        {
            var chars = s.ToArray();
            for (int i = 0, j = s.Length - 1; i < j;)
            {
                if (!IsVowel(chars[i]))
                {
                    i++;
                }

                if (!IsVowel(chars[j]))
                {
                    j--;
                }

                if (i >= j)
                {
                    break;
                }

                if (IsVowel(chars[i]) && IsVowel(chars[j]))
                {
                    chars[i] = s[j];
                    chars[j] = s[i];
                    i++;
                    j--;
                }
            }

            return new string(chars);

        }

        private bool IsVowel(char c)
        {
            return c == 'a' || c == 'A' || 
                   c == 'e' || c == 'E' || 
                   c == 'i' || c == 'I' || 
                   c == 'o' || c == 'O' ||
                   c == 'u' || c == 'U';
        }
    }
}